<?php
/** @see Zend_Controller_Action */

class ErrorController extends Zend_Controller_Action {
	public function init() {
		$this->view->headTitle(_t('Błąd'), 'PREPEND');
	}
	
	/**
	 * Akcja błędu aplikacji lub nieznalezienia strony
	 *
	 */
	public function errorAction() {
		$errors = $this->_getParam('error_handler');
		$session = new Zend_Session_Namespace('error');
		
		if (!empty($errors)) {
			//zapiszmy to w logach:
			Zend_Registry::get('Zend_Log')->info('Error ' . $errors->type . (isset($errors->exception) ? '[' . $errors->exception->getMessage() . ']' : '') . '#: "' . $errors->request->getRequestUri() . '" from ' . $_SERVER['REMOTE_ADDR']);
			switch ($errors->type) {
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
					// 404 error -- controller or action not found
					$this->getResponse()->setHttpResponseCode(404);
					$this->view->type = 404;
					break;
				default:
					// application error
					// zapisz duzo fajnych rzeczy
					$config = Zend_Registry::get('config');
					$logFilename = date('/Y/m/d') . date('.His,u');
					$logDirFilename = rtrim($config->environment->log, '/') . $logFilename;
					if ($config->environment->log && is_dir($config->environment->log) && is_writable($config->environment->log)) {
						if (is_dir(dirname($logDirFilename)) && is_writable(dirname($logDirFilename))) {
							if (file_put_contents($logDirFilename . '.dump',
								'$$$ Dump created on ' . date('c') . ' $$$' . PHP_EOL .
								'# URL: ' . PHP_EOL . print_r($errors->request->getRequestUri(), true) . PHP_EOL .
								'# EXCEPTION: ' . PHP_EOL . print_r($errors->exception, true) . PHP_EOL .
								'# GET: ' . PHP_EOL . print_r($_GET, true) . PHP_EOL .
								'# POST: ' . PHP_EOL . print_r($_POST, true) . PHP_EOL .
								'# COOKIE: ' . PHP_EOL . print_r($_COOKIE, true) . PHP_EOL .
								'# ZEND_ENV: ' . PHP_EOL . print_r($this->getInvokeArg('env'), true) . PHP_EOL .
								'# ENV: ' . PHP_EOL . print_r($_ENV, true) . PHP_EOL .
								'# SESSION: ' . PHP_EOL . print_r($_SESSION, true) . PHP_EOL .
								'# SERVER: ' . PHP_EOL . print_r($_SERVER, true) . PHP_EOL .
								'# BACKTRACE: ' . PHP_EOL . print_r(debug_backtrace(), true)
							))
								$session->filename = $logDirFilename;
						}
					}
					$this->getResponse()->setHttpResponseCode(500);
					$this->view->type = 500;
					Zend_Registry::get('Zend_Log')->err('Application error: (' . get_class($errors->exception) . ') "' . $errors->exception->getMessage() . '" at ' . $errors->exception->getFile() . '#' . $errors->exception->getLine());
					break;
			}
			$this->view->env       = $this->getInvokeArg('env');
			$this->view->exception = $errors->exception;
			$this->view->request   = $errors->request;
		}
		
		if (isset($session->filename) && !empty($session->filename)) {
			$Default_Form_Errors = new Default_Form_Errors();
			$form = $Default_Form_Errors->feedback();
			if ($this->getRequest()->isPost()) {
				if ($form->isValid($this->getRequest()->getPost())) { // poprawny
					file_put_contents($session->filename . '.desc',
						'$$$ Description created on ' . date('c') . ' $$$' . PHP_EOL .
						print_r($form->getValues(), true));
					$session->unsetAll();
					$this->_helper->FlashMessenger(_t('Dziękujemy, błąd został zgłoszony'));
					$this->_redirect($this->getFrontController()->getRouter()->assemble(array(), 'default', true));
				}//else
				//	$this->_helper->FlashMessenger(_t('Popraw niżej zaznaczone błędy w formularzu'));
			}
			$this->view->form = $form;
		}
	}
	
	/**
	 * Akcja dla odmowy dostępu
	 *
	 */
	public function deniedAction() {
		$this->view->headTitle(_t('Dostęp zabroniony'), 'PREPEND');
		
		//zapiszmy to w logach:
		Zend_Registry::get('Zend_Log')->info('Error EXCEPTION_NO_ACCESS#: "' . $this->getFrontController()->getRequest()->getRequestUri() . '" from ' . $_SERVER['REMOTE_ADDR']);
		
		// strona logowania
		$this->getResponse()->setHttpResponseCode(403);
		if (!Zend_Auth::getInstance()->hasIdentity()) {
			$this->_helper->FlashMessenger(_t('Zaloguj się, aby kontynuować. Jeśli nie masz konta, możesz je założyć w ciągu mniej niż dwóch minut!'));
			$this->_redirect($this->getFrontController()->getRouter()->assemble(array('controller' => 'user', 'action' => 'login', 'url' => urlencode(ltrim($this->getFrontController()->getRouter()->assemble(array()), '/'))), 'default', true));
		}
	}
}